<?php
/*+*******************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
********************************************************************************/
class HelpDeskHandler extends VTEventHandler {
function __getSendToEmail($crmid) {
if(empty($crmid)) return false;
$sendtoemail = false;
global $adb;
$metaresult = $adb->pquery("SELECT setype FROM vtiger_crmentity WHERE crmid=? AND deleted = 0", array($crmid));
if($metaresult && $adb->num_rows($metaresult)) {$metaresultrow = $adb->fetch_array($metaresult);
$emailres = false;
if($metaresultrow['setype'] == 'Contacts') {
$emailres = $adb->pquery("SELECT email,yahooid FROM vtiger_contactdetails WHERE contactid = ?", array($crmid));
} else if($metaresultrow['setype'] == 'Accounts') {
$emailres = $adb->pquery("SELECT email1,email2 FROM vtiger_account WHERE accountid = ?", array($crmid));
}
if($emailres && $adb->num_rows($emailres)) {
$emailresrow = $adb->fetch_array($emailres);
if(!empty($emailresrow[0])) $sendtoemail = $emailresrow[0];
if(!empty($emailresrow[1])) $sendtoemail = $emailresrow[1];
}
}
return $sendtoemail;
}
function handleEvent($eventName, $entityData) {
global $log, $adb;
if($eventName == 'vtiger.entity.aftersave') {
$moduleName = $entityData->getModuleName();
// Event not related to HelpDesk - IGNORE
if($moduleName != 'HelpDesk') {
return;
}
// Take action if the service running is MailScanner (either via Cron/Scan Now)
if(isset($_REQUEST) && $_REQUEST['service'] == 'MailScanner' ) {
$focus = $entityData->focus;
$sendToEmail = $this->__getSendToEmail($focus->column_fields['parent_id']);
// If the entity is create new and we know whom to send the mail proceed.
if($entityData->isNew() && $sendToEmail) {
global $HELPDESK_SUPPORT_EMAIL_ID, $HELPDESK_SUPPORT_NAME, $HELPDESK_SUPPORT_EMAIL_REPLY_ID; include_once 'vtlib/Vtiger/Mailer.php';
$mailer = new Vtiger_Mailer();
$mailer->ConfigSenderInfo($HELPDESK_SUPPORT_EMAIL_ID, $HELPDESK_SUPPORT_NAME);
$mailer->AddReplyTo($HELPDESK_SUPPORT_EMAIL_REPLY_ID);
$mailer->initFromTemplate('Auto Ticket First Response Template');
// Update the email subject
$mailer->Subject = sprintf("%s [ Ticket Id : %s ] Re : %s", $focus->column_fields['ticket_no'], $focus->id, $focus->column_fields['ticket_title']);
$mailer->SendTo( $sendToEmail, '', false, false, true );
}
}
}
}
}
?>
